package com.situ.company.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;

public class JDBCUtil {
	private JDBCUtil() {

	}

	// 定义成私有的是在其他类使用静态方法时不会类的实例化
	private static ResourceBundle rb = ResourceBundle.getBundle("com.situ.company.util.jdbc");
	private static String driver = rb.getString("driver");
	private static String url = rb.getString("url");
	private static String user = rb.getString("user");
	private static String pass = rb.getString("pass");

	// 1-加载驱动
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// 2-获得链接
	public static Connection getConnertion() {
		try {
			return DriverManager.getConnection(url, user, pass);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 关闭链接和状态集
	public static void close(Connection conn, Statement st) {
		close(conn, st, null);
	}

	// 关闭链接、状态集、结果集
	public static void close(Connection conn, Statement st, ResultSet rs) {

		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (st != null) {
				st.close();
				st = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Integer update(String sql, Object... obj) {
		return update(sql, Arrays.asList(obj));
		// obj数组中元素类型是引用类型-->asList返回的集合中的元素是obj数组中的每个元素
		// obj数组中元素类型是基本数据类型-->asList返回的list集合中的元素仅有一个，即obj这个数组作为list集合中的这个元素
	}

	/**
	 * 增删改DML
	 * @param sql
	 * @param obj
	 * @return
	 */
	public static Integer update(String sql, List<Object> obj) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = getConnertion();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < obj.size(); i++)
				ps.setObject(i + 1, obj.get(i));
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(con, ps);
		}
		return null;
	}

	/**
	 * 查DQL
	 * @param sql
	 * @param obj
	 * @param clazz
	 *            返回的集合中的元素类型
	 * @param fields
	 *            key=属性名value=字段名
	 * @return
	 */
	public static <T> List<T> query(String sql, List<Object> obj, Class<T> clazz, Map<String, String> fields) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<T> result = new ArrayList<>();
		try {
			con = getConnertion();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < obj.size(); i++)
				ps.setObject(i + 1, obj.get(i));
			rs = ps.executeQuery();
			while (rs.next()) {
				T model = clazz.newInstance();
				for (Entry<String, String> entry : fields.entrySet()) {
					Field field = clazz.getDeclaredField(entry.getKey());
					field.setAccessible(true);
					field.set(model, rs.getObject(entry.getValue()));
				}
				result.add(model);
			}
		} catch (SQLException | InstantiationException | IllegalAccessException | NoSuchFieldException
				| SecurityException e) {
			e.printStackTrace();
		} finally {
			close(con, ps, rs);
		}
		return result;
	}
}
